<?php

namespace app\index\controller;

use app\index\model\userToken as UserTokenModel;

class UserToken 
{
    public function saveUserToken($openid, $token)
    {
        
        $message = "";
        if (UserTokenModel::get(["userToken" => $token]) == NULL) {
            //不存在，新增
            $userToken = new UserTokenModel;
            $userToken["userToken"] = $token;
            $userToken["openId"] = $openid; 
            if ($userToken->isUpdate(false)->save()) {
                $message .= "add userToken success.";
            }   
            else {
                $message .= "add userToken fail(not change).";
            }
        }
        else {
            //存在，保存
            $userToken = UserTokenModel::get(["openId" => $openid]);
            $userToken["userToken"] = $token; 
            if ($userToken->save()) {
                $message .= "update userToken success.";
            }    
            else {
                $message .= "update userToken fail(not change).";
            }
        }
        return $message;
    }
}